package com.privatetime.common.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

public abstract class AbstractBodyTagSupport extends BodyTagSupport {

	private static final long serialVersionUID = 1L;
	
	protected final Logger logger = Logger.getLogger(getClass());
	
	protected static final String NEW_LINE = System.getProperty("line.separator");

	/**
	 * body content between start tag and end tag
	 */
	protected String content = null;

	protected String doStartContent = "";

	public AbstractBodyTagSupport() {

	}

	@Override
	public int doAfterBody() throws JspException {
		try {
			if (bodyContent != null) {
				String value = bodyContent.getString();

				if (value == null) {
					value = "";
				}

				this.content = value.trim();
			} else {
				this.content = "";
			}
		} catch (Throwable t) {
			throw new JspException("Exception processing doAfterBody():"
					+ t.getMessage(), t);
		}

		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			end();
			return EVAL_PAGE;
		} catch (Throwable t) {
			throw new JspException("Exception processing doEndTag():"
					+ t.getMessage(), t);
		}
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			start();
			return EVAL_BODY_INCLUDE;
		} catch (Throwable t) {
			throw new JspException("Exception processing doStartTag():"
					+ t.getMessage(), t);
		}
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		super.release();
	}

	@Override
	public void doInitBody() throws JspException {
		// TODO Auto-generated method stub
		super.doInitBody();
	}

	protected abstract void start();

	protected abstract void end() throws JspException;

	public String getDoStartContent() {
		return doStartContent;
	}
}
